import wxToPromise from "./wx";
import APIConfig from "../config/api";
import Http from "./http";
import cache from "../enum/cache";
const token = wx.getStorageSync(cache.TOKEN)
class FileUploader extends Http {
    static async upload(filePath, key = 'file') {
        let res
        try {
            res = await wxToPromise('uploadFile', {
                url: APIConfig.baseUrl + '/minio/UploadByExpertId',
                filePath: filePath,
                name: 'file',
                formData: {
                  bucketName: 'touxiang',
                  expertId: 'f165f42f5b3149c6bddd2dbee8c3d655',
                },
                header: {
                  'Authorization': `Bearer ${token}`
                },
            })
        } catch (e) {
            FileUploader._showError(-1)
            throw new Error(e.errMsg)
        }
        const serverData = JSON.parse(res.data)
        
        if (res.statusCode !== 200) {
            FileUploader._showError(res.code, res.msg)
            throw new Error(res.msg)
        }

        return serverData.data
    }
    // 文件清单文件上传
  static async uploadFile(filePath, data) {
        let res
        try {
            res = await wxToPromise('uploadFile', {
                url: APIConfig.baseUrl + '/minioApp/uploadAndSave',
                filePath: filePath,
                name: 'file',
                formData: data,
                header: {
                  'Authorization': `Bearer ${token}`
                },
            })
            console.log(res);
        } catch (e) {
            FileUploader._showError(-1)
            throw new Error(e.errMsg)
        }
        const serverData = JSON.parse(res.data)
        
        if (res.statusCode !== 200) {
            FileUploader._showError(serverData.error_code, serverData.message)
            throw new Error(serverData.message)
        }
        console.log(serverData.data);
        return serverData.data
    }
    // 专家 附件上传
    static async uploadExpertFile(filePath, data) {
      let res
      try {
          res = await wxToPromise('uploadFile', {
              url: APIConfig.baseUrl + '/minioApp/uploadFileExpertOpinion',
              filePath: filePath,
              name: 'file',
              formData: data,
              header: {
                'Authorization': `Bearer ${token}`
              },
          })
          console.log(res);
      } catch (e) {
          FileUploader._showError(-1)
          throw new Error(e.errMsg)
      }
      const serverData = JSON.parse(res.data)
      
      if (res.statusCode !== 200) {
        FileUploader._showError(serverData.code, serverData.msg)
        throw new Error(serverData.msg)
      }
      if(serverData.code !== 200) {
        FileUploader._showError(serverData.code, serverData.msg)
        throw new Error(serverData.msg)
      }
      return serverData.data
  }
    // 讨论区 附件上传
    static async uploadDiscussFile(filePath, data) {
      let res
      try {
          res = await wxToPromise('uploadFile', {
              url: APIConfig.baseUrl + '/minioApp/uploadFileDiscuss',
              filePath: filePath,
              name: 'file',
              formData: data,
              header: {
                'Authorization': `Bearer ${token}`
              },
          })
          console.log(res);
      } catch (e) {
          FileUploader._showError(-1)
          throw new Error(e.errMsg)
      }
      const serverData = JSON.parse(res.data)
      
      if (res.statusCode !== 200) {
        FileUploader._showError(serverData.code, serverData.msg)
        throw new Error(serverData.msg)
      }
      if(serverData.code !== 200) {
        FileUploader._showError(serverData.code, serverData.msg)
        throw new Error(serverData.msg)
      }
      return serverData
  }
}

export default FileUploader
